package net.sunofbeach.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sunofbeach.blog.mapper.LabelMapper;
import net.sunofbeach.blog.pojo.Label;
import net.sunofbeach.blog.pojo.query.LabelQuery;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.service.LabelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements LabelService {

    /**
     * 条件分页查询标签
     *
     * @param pageParam
     * @param labelQuery
     * @return
     */
    @Override
    public IPage<Label> selectPage(Page<Label> pageParam, LabelQuery labelQuery) {
        QueryWrapper<Label> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(labelQuery.getName())) {
            wrapper.like("name", labelQuery.getName());
        }
        if (labelQuery.getCount() != null) {
            wrapper.eq("count", labelQuery.getCount());
        }
        return baseMapper.selectPage(pageParam, wrapper);
    }


    /**
     * 获取标签云
     *
     * @param size
     * @return
     */
    @Override
    public Result listLabels(int size) {
        Page<Label> pageParam = new Page<>(1, size);
        Page<Label> labelPage = baseMapper.selectPage(pageParam, null);
        return Result.ok(labelPage);
    }
}
